   1              		.syntax unified
   2              		.cpu cortex-m3
   3              		.fpu softvfp
   4              		.eabi_attribute 20, 1
   5              		.eabi_attribute 21, 1
   6              		.eabi_attribute 23, 3
   7              		.eabi_attribute 24, 1
   8              		.eabi_attribute 25, 1
   9              		.eabi_attribute 26, 1
  10              		.eabi_attribute 30, 4
  11              		.eabi_attribute 34, 1
  12              		.eabi_attribute 18, 4
  13              		.thumb
  14              		.syntax unified
  15              		.file	"modbus_slave.c"
  16              		.text
  17              	.Ltext0:
  18              		.cfi_sections	.debug_frame
  19              		.section	.text.modbus_SendPacketException,"ax",%progbits
  20              		.align	1
  21              		.thumb
  22              		.thumb_func
  24              	modbus_SendPacketException:
  25              	.LFB4:
  26              		.file 1 "uc_libc_dev/protocol/modbus/modbus_slave.c"
   1:uc_libc_dev/protocol/modbus/modbus_slave.c **** /* Criado em 17/03/2015
   2:uc_libc_dev/protocol/modbus/modbus_slave.c ****  * Atualização:
   3:uc_libc_dev/protocol/modbus/modbus_slave.c ****  * 	12/08/2016 - Retirado controle de tempo pela função now
   4:uc_libc_dev/protocol/modbus/modbus_slave.c ****  * 	29/04/2015
   5:uc_libc_dev/protocol/modbus/modbus_slave.c ****  *
   6:uc_libc_dev/protocol/modbus/modbus_slave.c ****  *
   7:uc_libc_dev/protocol/modbus/modbus_slave.c ****  *
   8:uc_libc_dev/protocol/modbus/modbus_slave.c ****  * Biblioteca MODBUS escravo barramento serial rs232/rs485 no modo RTU (Binário)
   9:uc_libc_dev/protocol/modbus/modbus_slave.c ****  *
  10:uc_libc_dev/protocol/modbus/modbus_slave.c ****  * Este protocolo atende somente as funções:
  11:uc_libc_dev/protocol/modbus/modbus_slave.c ****  * 	 Leituta de muitos registradores, código 3,
  12:uc_libc_dev/protocol/modbus/modbus_slave.c ****  * 	 Escrita em um simples registrador, código 6
  13:uc_libc_dev/protocol/modbus/modbus_slave.c ****  * 	 Escrita em muitos registradores, código 16
  14:uc_libc_dev/protocol/modbus/modbus_slave.c ****  *
  15:uc_libc_dev/protocol/modbus/modbus_slave.c ****  * Esta lib somente captura as mensagens quando o barramento serial fique em silencio no minimo 5ms
  16:uc_libc_dev/protocol/modbus/modbus_slave.c ****  * Então, o timeout do mestre na espera de uma resposta deste dispostivo deve ser superior a 5ms.
  17:uc_libc_dev/protocol/modbus/modbus_slave.c ****  * Geralmente usamos 3 segundos.
  18:uc_libc_dev/protocol/modbus/modbus_slave.c ****  * Entretanto, o mestre pode se comunicar com outros dispositivos que utilizam a técnica t35,
  19:uc_libc_dev/protocol/modbus/modbus_slave.c ****  * onde o tempo de silencio do barramento é muito menor. Neste caso, esta lib vai ignorar as mensag
  20:uc_libc_dev/protocol/modbus/modbus_slave.c ****  * por considerar que as mensagens são destinadas para outros dispositivos.
  21:uc_libc_dev/protocol/modbus/modbus_slave.c ****  *
  22:uc_libc_dev/protocol/modbus/modbus_slave.c ****  * Os endereços dos registradores devem ser passados pelos seus valores reais e não por valores enu
  23:uc_libc_dev/protocol/modbus/modbus_slave.c ****  *  ou seja, valores de 0 a N.
  24:uc_libc_dev/protocol/modbus/modbus_slave.c ****  *
  25:uc_libc_dev/protocol/modbus/modbus_slave.c ****  * Técnica t35 = Tempo do barramento em silêncio que é 3.5 vezes o tamanho de um char no barramento
  26:uc_libc_dev/protocol/modbus/modbus_slave.c ****  *
  27:uc_libc_dev/protocol/modbus/modbus_slave.c ****  * Para detalhes do protocolo consunte o documento D:\meus_conhecimentos\_devices_misc__\modbus\res
  28:uc_libc_dev/protocol/modbus/modbus_slave.c ****  *
  29:uc_libc_dev/protocol/modbus/modbus_slave.c ****  * */
  30:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
  31:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
  32:uc_libc_dev/protocol/modbus/modbus_slave.c **** #include "modbus_slave.h"
  33:uc_libc_dev/protocol/modbus/modbus_slave.c **** #if (MODBUS_USE_DEBUG == pdON)
  34:uc_libc_dev/protocol/modbus/modbus_slave.c **** #include "stdio_uc.h"
  35:uc_libc_dev/protocol/modbus/modbus_slave.c **** #endif
  36:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
  37:uc_libc_dev/protocol/modbus/modbus_slave.c **** typedef struct {
  38:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	int slaveID;						// Endereço desse dispostivo no barramento modbus
  39:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	int (*puts)(u8 *buffer, u16 count);	// Ponteiro da função para envio de bytes
  40:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	int (*getc)(u8 *ch);				// Ponteiro da função para recebimento de bytes
  41:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	int (*byteAvailable)(void);			// Ponteiro da função para checagem se há bytes já recebidos
  42:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	void(*flushRX)(void);				// Ponteiro da função para limpar o buffer de recepção
  43:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	volatile uint timeout; 				// Determina o tempo de silencio do barramento
  44:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
  45:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	// vars aux para comunicação atual
  46:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	u8 query[256];						// Buffer aux de transmissão e recepção de dados
  47:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	int cmd;							// Comando (função) solicitado
  48:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	int lastLenBuffer;
  49:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
  50:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	// Funções externas para antender os comandos recebidos
  51:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	int (*read_regs)(uint addrInit,  u8* query, uint count);
  52:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	int (*write_reg)(uint addr, u16 value);
  53:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	int (*write_regs)(uint addrInit, u8* query, uint count);
  54:uc_libc_dev/protocol/modbus/modbus_slave.c **** } modbusSlave_t;
  55:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
  56:uc_libc_dev/protocol/modbus/modbus_slave.c **** static modbusSlave_t modbus;
  57:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
  58:uc_libc_dev/protocol/modbus/modbus_slave.c **** static int modbus_GetPacket(u8* query);
  59:uc_libc_dev/protocol/modbus/modbus_slave.c **** static void modbus_SendPacketException(int exception);
  60:uc_libc_dev/protocol/modbus/modbus_slave.c **** static void modbus_SendPacketRegs(u8* query, int len);
  61:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
  62:uc_libc_dev/protocol/modbus/modbus_slave.c **** // ------------------------------------------------------------------------------------------------
  63:uc_libc_dev/protocol/modbus/modbus_slave.c **** // FUNÇÃO:		modbus_SlaveInit
  64:uc_libc_dev/protocol/modbus/modbus_slave.c **** // Descrição: 	Inicializa o protocolo modbus no modo escravo
  65:uc_libc_dev/protocol/modbus/modbus_slave.c **** // Parametros:	slaveID: Endereço do escravo
  66:uc_libc_dev/protocol/modbus/modbus_slave.c **** //				puts_func:	Ponteiro da função de transmissão serial
  67:uc_libc_dev/protocol/modbus/modbus_slave.c **** //				getc_func:	Ponteiro da função de recepção de dados
  68:uc_libc_dev/protocol/modbus/modbus_slave.c **** //				byte_available_func: Ponteiro da função para verificar se há dados no buffer de recepção seri
  69:uc_libc_dev/protocol/modbus/modbus_slave.c **** //				flushRX_func: Ponteiro da função que limpar os buffers seriais
  70:uc_libc_dev/protocol/modbus/modbus_slave.c **** // Retorna:		Nada
  71:uc_libc_dev/protocol/modbus/modbus_slave.c **** // ------------------------------------------------------------------------------------------------
  72:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
  73:uc_libc_dev/protocol/modbus/modbus_slave.c **** // ATENÇÃO: O tamanho do buffers de recepção e transmissão deve ter no mínimo o frame do modbus, re
  74:uc_libc_dev/protocol/modbus/modbus_slave.c **** // se usar rs485 tem que ter cuidado para fazer a inversão do barramento. exemplo
  75:uc_libc_dev/protocol/modbus/modbus_slave.c **** //		int uart_PutString(u8* buffer, u16 count) {
  76:uc_libc_dev/protocol/modbus/modbus_slave.c **** //			rs485_ENTX_ON = rs485_ENTX;
  77:uc_libc_dev/protocol/modbus/modbus_slave.c **** //			int ret = uart1_WriteTx(buffer, count);
  78:uc_libc_dev/protocol/modbus/modbus_slave.c **** //			while (!uart1_EmptyTx()); // Vamos esperar que o serial envie a mensagem
  79:uc_libc_dev/protocol/modbus/modbus_slave.c **** //			rs485_ENTX_OFF = rs485_ENTX;
  80:uc_libc_dev/protocol/modbus/modbus_slave.c **** //			return ret;
  81:uc_libc_dev/protocol/modbus/modbus_slave.c **** //		}
  82:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
  83:uc_libc_dev/protocol/modbus/modbus_slave.c **** void modbus_SlaveInit(
  84:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	int slaveID,
  85:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	int(*puts_func)(u8* buffer, u16 count),
  86:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	int(*getc_func)(u8* ch),
  87:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	int(*byte_available_func)(void),
  88:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	void(*flushRX_func)(void)
  89:uc_libc_dev/protocol/modbus/modbus_slave.c **** ) {
  90:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
  91:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.slaveID = slaveID;
  92:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.puts = puts_func;
  93:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.getc = getc_func;
  94:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.byteAvailable = byte_available_func;
  95:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.flushRX = flushRX_func;
  96:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.lastLenBuffer = 0;
  97:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.timeout = 0;
  98:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
  99:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.read_regs = NULL;
 100:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.write_reg = NULL;
 101:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.write_regs = NULL;
 102:uc_libc_dev/protocol/modbus/modbus_slave.c **** }
 103:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 104:uc_libc_dev/protocol/modbus/modbus_slave.c **** void modbus_SlaveSetID(int slaveID) {
 105:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.slaveID = slaveID;
 106:uc_libc_dev/protocol/modbus/modbus_slave.c **** }
 107:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 108:uc_libc_dev/protocol/modbus/modbus_slave.c **** // ------------------------------------------------------------------------------------------------
 109:uc_libc_dev/protocol/modbus/modbus_slave.c **** // Descrição: 	Aponta para as funções de controle de acesso aos registradores
 110:uc_libc_dev/protocol/modbus/modbus_slave.c **** // Parametros:	readregs_func: Lê N valores dos registradores, ver mais detalhes abaixo
 111:uc_libc_dev/protocol/modbus/modbus_slave.c **** //				writereg_func:	Escreve um valor em um registrador de 16 bits
 112:uc_libc_dev/protocol/modbus/modbus_slave.c **** //				writeregs_func: Escreve N valores nos registradores, lembrando que o endereçamento de registr
 113:uc_libc_dev/protocol/modbus/modbus_slave.c **** // Retorna:		Código MODBUS, ver abaixo
 114:uc_libc_dev/protocol/modbus/modbus_slave.c **** // ------------------------------------------------------------------------------------------------
 115:uc_libc_dev/protocol/modbus/modbus_slave.c **** // as funções devem ter a seguinte forma:
 116:uc_libc_dev/protocol/modbus/modbus_slave.c **** //		readregs_func: Lê N valores dos registradores, lembrando que o endereçamento de registradores d
 117:uc_libc_dev/protocol/modbus/modbus_slave.c **** //			int nomeFunc(uint addrInit, u8* query, uint count);
 118:uc_libc_dev/protocol/modbus/modbus_slave.c **** //				addrInit: Endereço do primeiro registrador de 16 bits a ser acessado
 119:uc_libc_dev/protocol/modbus/modbus_slave.c **** //				query: Buffer para ser armazenado os valores dos registradores para serem enviados ao mestre
 120:uc_libc_dev/protocol/modbus/modbus_slave.c **** //				count: Quantidade de registradores devem ser lidos
 121:uc_libc_dev/protocol/modbus/modbus_slave.c **** //		writereg_func: Escreve um valor em um registrador de 16 bits
 122:uc_libc_dev/protocol/modbus/modbus_slave.c **** //			int nomeFunc(uint addr, u16 value);
 123:uc_libc_dev/protocol/modbus/modbus_slave.c **** //				addr: Endereço do registrador de 16 bits a ser gravado
 124:uc_libc_dev/protocol/modbus/modbus_slave.c **** //				value: Valor a ser gravado no registrador
 125:uc_libc_dev/protocol/modbus/modbus_slave.c **** //		writeregs_func: Escreve N valores nos registradores, lembrando que o endereçamento de registrad
 126:uc_libc_dev/protocol/modbus/modbus_slave.c **** //			int nomeFunc(uint addrInit, u8* query, uint count);
 127:uc_libc_dev/protocol/modbus/modbus_slave.c **** //				addrInit: Endereço do primeiro registrador de 16 bits a ser gravado
 128:uc_libc_dev/protocol/modbus/modbus_slave.c **** //				query: Buffer com os valores dos registradores a serem salvos
 129:uc_libc_dev/protocol/modbus/modbus_slave.c **** //				count: Quantidade de registradores devem ser gravados
 130:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 131:uc_libc_dev/protocol/modbus/modbus_slave.c **** // 	AS funções devem retornar com os seguintes valores:
 132:uc_libc_dev/protocol/modbus/modbus_slave.c **** //		modbusNO_ERROR:  Processamento feito com sucesso
 133:uc_libc_dev/protocol/modbus/modbus_slave.c **** //		modbusILLEGAL_DATA_ADDRESS:	Endereço de registrador inexistente
 134:uc_libc_dev/protocol/modbus/modbus_slave.c **** //		modbusILLEGAL_DATA_VALUE: Valor a ser gravado é inválido
 135:uc_libc_dev/protocol/modbus/modbus_slave.c **** //		modbusSLAVE_DEVICE_FAILURE:	Um irrecuperável erro ocorreu enquanto o servidor estava tentando e
 136:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 137:uc_libc_dev/protocol/modbus/modbus_slave.c **** // EXEMPLOS VEJA NO FIM DO ARQUIVO
 138:uc_libc_dev/protocol/modbus/modbus_slave.c **** void modbus_SlaveAppendFunctions(
 139:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	int (*readregs_func)(uint addrInit, u8* query, uint count),
 140:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	int (*writereg_func)(uint addr, u16 value),
 141:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	int (*writeregs_func)(uint addrInit, u8* query, uint count)
 142:uc_libc_dev/protocol/modbus/modbus_slave.c **** ) {
 143:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.read_regs = readregs_func;
 144:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.write_reg = writereg_func;
 145:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.write_regs = writeregs_func;
 146:uc_libc_dev/protocol/modbus/modbus_slave.c **** }
 147:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 148:uc_libc_dev/protocol/modbus/modbus_slave.c **** // ------------------------------------------------------------------------------------------------
 149:uc_libc_dev/protocol/modbus/modbus_slave.c **** // FUNÇÃO:		modbus_GetPacket
 150:uc_libc_dev/protocol/modbus/modbus_slave.c **** // Descrição: 	Captura o pacote de dados que estão o buffer da serial e já calcula o CRC
 151:uc_libc_dev/protocol/modbus/modbus_slave.c **** // Parametros:	query: Ponteiro do buffer para salvar o pacote
 152:uc_libc_dev/protocol/modbus/modbus_slave.c **** // Retorna:		retorna com a quantidade de bytes no buffer query, ou o valor do erro
 153:uc_libc_dev/protocol/modbus/modbus_slave.c **** // ------------------------------------------------------------------------------------------------
 154:uc_libc_dev/protocol/modbus/modbus_slave.c **** static int modbus_GetPacket(u8* query) {
 155:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	int len = 0;
 156:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	u8 ch;
 157:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 158:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	// capturo os dados do buffer da serial e passo para o buffer modbus
 159:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	// faço enquanto há dados no buffer de recepção
 160:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	#if (MODBUS_USE_DEBUG == pdON)
 161:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	plognp("MODBUS: GET PACKET: ");
 162:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	#endif
 163:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 164:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	while (modbus.getc(&ch) == pdPASS) {
 165:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		if (len > 255) return errMODBUS_BUFRX_OVERFLOW;
 166:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		query[len++] = ch;
 167:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		#if (MODBUS_USE_DEBUG == pdON)
 168:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		plognp("0x%x ", ch);
 169:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		#endif
 170:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	}
 171:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 172:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	// o menor pacote é de 4 bytes
 173:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	if (len < 4) {
 174:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		#if (MODBUS_USE_DEBUG == pdON)
 175:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		plognp(CMD_TERMINATOR);
 176:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		plognp("ERROR: len packet "CMD_TERMINATOR);
 177:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		#endif
 178:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 179:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		return errMODBUS_BUFRX_INCOMPLETE;
 180:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	}
 181:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 182:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	// processar CRC
 183:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	len-=2; // desconsidera o CRC
 184:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	uint crc = crc16_MODBUS(query, len);
 185:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	uint crc_rx = ( query[len+1] << 8) | (query[len]);
 186:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 187:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	#if (MODBUS_USE_DEBUG == pdON)
 188:uc_libc_dev/protocol/modbus/modbus_slave.c ****     plognp(CMD_TERMINATOR);
 189:uc_libc_dev/protocol/modbus/modbus_slave.c ****     plognp("MODBUS: len %d ", len);
 190:uc_libc_dev/protocol/modbus/modbus_slave.c ****     plognp("query: ");
 191:uc_libc_dev/protocol/modbus/modbus_slave.c ****     int db_x; for(db_x=0;db_x<len;db_x++) plognp("0x%x ", query[db_x]);
 192:uc_libc_dev/protocol/modbus/modbus_slave.c ****     plognp(CMD_TERMINATOR);
 193:uc_libc_dev/protocol/modbus/modbus_slave.c ****     plognp("MODBUS: crc 0x%x == 0x%x"CMD_TERMINATOR, crc_rx, crc);
 194:uc_libc_dev/protocol/modbus/modbus_slave.c ****     #endif
 195:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 196:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	if (crc != crc_rx) return errMODBUS_CRC;
 197:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 198:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	return len;
 199:uc_libc_dev/protocol/modbus/modbus_slave.c **** }
 200:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 201:uc_libc_dev/protocol/modbus/modbus_slave.c **** // ------------------------------------------------------------------------------------------------
 202:uc_libc_dev/protocol/modbus/modbus_slave.c **** // FUNÇÃO:		modbus_SendPacketException
 203:uc_libc_dev/protocol/modbus/modbus_slave.c **** // Descrição: 	Envia uma mensagem de exceção para o mestre
 204:uc_libc_dev/protocol/modbus/modbus_slave.c **** // Parametros:	exception: Código da exceção
 205:uc_libc_dev/protocol/modbus/modbus_slave.c **** // Retorna:		Nada
 206:uc_libc_dev/protocol/modbus/modbus_slave.c **** // ------------------------------------------------------------------------------------------------
 207:uc_libc_dev/protocol/modbus/modbus_slave.c **** static void modbus_SendPacketException(int exception) {
  27              		.loc 1 207 0
  28              		.cfi_startproc
  29              		@ args = 0, pretend = 0, frame = 0
  30              		@ frame_needed = 0, uses_anonymous_args = 0
  31              	.LVL0:
  32 0000 70B5     		push	{r4, r5, r6, lr}
  33              	.LCFI0:
  34              		.cfi_def_cfa_offset 16
  35              		.cfi_offset 4, -16
  36              		.cfi_offset 5, -12
  37              		.cfi_offset 6, -8
  38              		.cfi_offset 14, -4
 208:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.query[0] = modbus.slaveID;
  39              		.loc 1 208 0
  40 0002 0B4C     		ldr	r4, .L2
 209:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.query[1] = 0x80 + modbus.cmd;
 210:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.query[2] = exception;
 211:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	uint crc = crc16_MODBUS(modbus.query, 3);
  41              		.loc 1 211 0
  42 0004 0321     		movs	r1, #3
 208:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.query[0] = modbus.slaveID;
  43              		.loc 1 208 0
  44 0006 2368     		ldr	r3, [r4]
  45              		.loc 1 211 0
  46 0008 04F11805 		add	r5, r4, #24
 208:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.query[0] = modbus.slaveID;
  47              		.loc 1 208 0
  48 000c 2376     		strb	r3, [r4, #24]
 209:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.query[1] = 0x80 + modbus.cmd;
  49              		.loc 1 209 0
  50 000e D4F81831 		ldr	r3, [r4, #280]
 210:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	uint crc = crc16_MODBUS(modbus.query, 3);
  51              		.loc 1 210 0
  52 0012 A076     		strb	r0, [r4, #26]
 209:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.query[1] = 0x80 + modbus.cmd;
  53              		.loc 1 209 0
  54 0014 803B     		subs	r3, r3, #128
  55              		.loc 1 211 0
  56 0016 2846     		mov	r0, r5
  57              	.LVL1:
 209:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.query[1] = 0x80 + modbus.cmd;
  58              		.loc 1 209 0
  59 0018 6376     		strb	r3, [r4, #25]
  60              		.loc 1 211 0
  61 001a FFF7FEFF 		bl	crc16_MODBUS
  62              	.LVL2:
 212:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.query[3] = (u8)(crc&0xFF);
  63              		.loc 1 212 0
  64 001e E076     		strb	r0, [r4, #27]
 213:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.query[4] = (u8)(crc>>8);
  65              		.loc 1 213 0
  66 0020 000A     		lsrs	r0, r0, #8
  67              	.LVL3:
  68 0022 2077     		strb	r0, [r4, #28]
 214:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 215:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.puts(modbus.query, 5);
  69              		.loc 1 215 0
  70 0024 6368     		ldr	r3, [r4, #4]
  71 0026 2846     		mov	r0, r5
 216:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 217:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	#if (MODBUS_USE_DEBUG == pdON)
 218:uc_libc_dev/protocol/modbus/modbus_slave.c ****     plognp("MODBUS: SEND EXCPETION: ");
 219:uc_libc_dev/protocol/modbus/modbus_slave.c ****     int db_x; for(db_x=0;db_x<5;db_x++) plognp("0x%x ", modbus.query[db_x]);
 220:uc_libc_dev/protocol/modbus/modbus_slave.c ****     plognp(CMD_TERMINATOR);
 221:uc_libc_dev/protocol/modbus/modbus_slave.c ****     plognp("MODBUS: crc 0x%x"CMD_TERMINATOR, crc);
 222:uc_libc_dev/protocol/modbus/modbus_slave.c ****     #endif
 223:uc_libc_dev/protocol/modbus/modbus_slave.c **** }
  72              		.loc 1 223 0
  73 0028 BDE87040 		pop	{r4, r5, r6, lr}
  74              	.LCFI1:
  75              		.cfi_restore 14
  76              		.cfi_restore 6
  77              		.cfi_restore 5
  78              		.cfi_restore 4
  79              		.cfi_def_cfa_offset 0
 215:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
  80              		.loc 1 215 0
  81 002c 0521     		movs	r1, #5
  82 002e 1847     		bx	r3
  83              	.LVL4:
  84              	.L3:
  85              		.align	2
  86              	.L2:
  87 0030 00000000 		.word	.LANCHOR0
  88              		.cfi_endproc
  89              	.LFE4:
  91              		.section	.text.modbus_SlaveInit,"ax",%progbits
  92              		.align	1
  93              		.global	modbus_SlaveInit
  94              		.thumb
  95              		.thumb_func
  97              	modbus_SlaveInit:
  98              	.LFB0:
  89:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
  99              		.loc 1 89 0
 100              		.cfi_startproc
 101              		@ args = 4, pretend = 0, frame = 0
 102              		@ frame_needed = 0, uses_anonymous_args = 0
 103              	.LVL5:
 104 0000 10B5     		push	{r4, lr}
 105              	.LCFI2:
 106              		.cfi_def_cfa_offset 8
 107              		.cfi_offset 4, -8
 108              		.cfi_offset 14, -4
  91:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.puts = puts_func;
 109              		.loc 1 91 0
 110 0002 084C     		ldr	r4, .L5
  94:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.flushRX = flushRX_func;
 111              		.loc 1 94 0
 112 0004 84E80F00 		stmia	r4, {r0, r1, r2, r3}
  95:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.lastLenBuffer = 0;
 113              		.loc 1 95 0
 114 0008 029B     		ldr	r3, [sp, #8]
 115              	.LVL6:
 116 000a 2361     		str	r3, [r4, #16]
  96:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.timeout = 0;
 117              		.loc 1 96 0
 118 000c 0023     		movs	r3, #0
 119 000e C4F81C31 		str	r3, [r4, #284]
  97:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 120              		.loc 1 97 0
 121 0012 6361     		str	r3, [r4, #20]
  99:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.write_reg = NULL;
 122              		.loc 1 99 0
 123 0014 C4F82031 		str	r3, [r4, #288]
 100:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.write_regs = NULL;
 124              		.loc 1 100 0
 125 0018 C4F82431 		str	r3, [r4, #292]
 101:uc_libc_dev/protocol/modbus/modbus_slave.c **** }
 126              		.loc 1 101 0
 127 001c C4F82831 		str	r3, [r4, #296]
 128 0020 10BD     		pop	{r4, pc}
 129              	.LVL7:
 130              	.L6:
 131 0022 00BF     		.align	2
 132              	.L5:
 133 0024 00000000 		.word	.LANCHOR0
 134              		.cfi_endproc
 135              	.LFE0:
 137              		.section	.text.modbus_SlaveSetID,"ax",%progbits
 138              		.align	1
 139              		.global	modbus_SlaveSetID
 140              		.thumb
 141              		.thumb_func
 143              	modbus_SlaveSetID:
 144              	.LFB1:
 104:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.slaveID = slaveID;
 145              		.loc 1 104 0
 146              		.cfi_startproc
 147              		@ args = 0, pretend = 0, frame = 0
 148              		@ frame_needed = 0, uses_anonymous_args = 0
 149              		@ link register save eliminated.
 150              	.LVL8:
 105:uc_libc_dev/protocol/modbus/modbus_slave.c **** }
 151              		.loc 1 105 0
 152 0000 014B     		ldr	r3, .L8
 153 0002 1860     		str	r0, [r3]
 154 0004 7047     		bx	lr
 155              	.L9:
 156 0006 00BF     		.align	2
 157              	.L8:
 158 0008 00000000 		.word	.LANCHOR0
 159              		.cfi_endproc
 160              	.LFE1:
 162              		.section	.text.modbus_SlaveAppendFunctions,"ax",%progbits
 163              		.align	1
 164              		.global	modbus_SlaveAppendFunctions
 165              		.thumb
 166              		.thumb_func
 168              	modbus_SlaveAppendFunctions:
 169              	.LFB2:
 142:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.read_regs = readregs_func;
 170              		.loc 1 142 0
 171              		.cfi_startproc
 172              		@ args = 0, pretend = 0, frame = 0
 173              		@ frame_needed = 0, uses_anonymous_args = 0
 174              		@ link register save eliminated.
 175              	.LVL9:
 143:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.write_reg = writereg_func;
 176              		.loc 1 143 0
 177 0000 034B     		ldr	r3, .L11
 178 0002 C3F82001 		str	r0, [r3, #288]
 144:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.write_regs = writeregs_func;
 179              		.loc 1 144 0
 180 0006 C3F82411 		str	r1, [r3, #292]
 145:uc_libc_dev/protocol/modbus/modbus_slave.c **** }
 181              		.loc 1 145 0
 182 000a C3F82821 		str	r2, [r3, #296]
 183 000e 7047     		bx	lr
 184              	.L12:
 185              		.align	2
 186              	.L11:
 187 0010 00000000 		.word	.LANCHOR0
 188              		.cfi_endproc
 189              	.LFE2:
 191              		.section	.text.modbus_SlaveProcess,"ax",%progbits
 192              		.align	1
 193              		.global	modbus_SlaveProcess
 194              		.thumb
 195              		.thumb_func
 197              	modbus_SlaveProcess:
 198              	.LFB6:
 224:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 225:uc_libc_dev/protocol/modbus/modbus_slave.c **** // ------------------------------------------------------------------------------------------------
 226:uc_libc_dev/protocol/modbus/modbus_slave.c **** // FUNÇÃO:		modbus_SendPacketException
 227:uc_libc_dev/protocol/modbus/modbus_slave.c **** // Descrição: 	Envia uma mensagem com os valores lidos dos registradores
 228:uc_libc_dev/protocol/modbus/modbus_slave.c **** // Parametros:	query: Buffer já contendo os valores dos registradores
 229:uc_libc_dev/protocol/modbus/modbus_slave.c **** //				len: Quantidade de registradores de 16 bits
 230:uc_libc_dev/protocol/modbus/modbus_slave.c **** // Retorna:		Nada
 231:uc_libc_dev/protocol/modbus/modbus_slave.c **** // ------------------------------------------------------------------------------------------------
 232:uc_libc_dev/protocol/modbus/modbus_slave.c **** static void modbus_SendPacketRegs(u8* query, int len) {
 233:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	query[0] = modbus.slaveID;
 234:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	query[1] = modbus.cmd;
 235:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	query[2] = (u8)(2*len);
 236:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	uint crc = crc16_MODBUS(query, (2*len)+3);
 237:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	query[(2*len)+3] = (u8)(crc&0xFF);
 238:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	query[(2*len)+4] = (u8)(crc>>8);
 239:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.puts(query, (2*len)+5);
 240:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 241:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	#if (MODBUS_USE_DEBUG == pdON)
 242:uc_libc_dev/protocol/modbus/modbus_slave.c ****     plognp("MODBUS: SEND PCK [%d]: ", len);
 243:uc_libc_dev/protocol/modbus/modbus_slave.c ****     int db_x; for(db_x=0;db_x<(2*len)+5;db_x++) plognp("0x%x ", query[db_x]);
 244:uc_libc_dev/protocol/modbus/modbus_slave.c ****     plognp(CMD_TERMINATOR);
 245:uc_libc_dev/protocol/modbus/modbus_slave.c ****     plognp("MODBUS: crc 0x%x"CMD_TERMINATOR, crc);
 246:uc_libc_dev/protocol/modbus/modbus_slave.c ****     #endif
 247:uc_libc_dev/protocol/modbus/modbus_slave.c **** }
 248:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 249:uc_libc_dev/protocol/modbus/modbus_slave.c **** // ------------------------------------------------------------------------------------------------
 250:uc_libc_dev/protocol/modbus/modbus_slave.c **** // FUNÇÃO:		modbus_SlaveProcess
 251:uc_libc_dev/protocol/modbus/modbus_slave.c **** // Descrição: 	Polling dde controle do modo escravo
 252:uc_libc_dev/protocol/modbus/modbus_slave.c **** // ------------------------------------------------------------------------------------------------
 253:uc_libc_dev/protocol/modbus/modbus_slave.c **** // exemplos de pacotes
 254:uc_libc_dev/protocol/modbus/modbus_slave.c **** // 		len	  	 0    1    2	3    4    5    6    7
 255:uc_libc_dev/protocol/modbus/modbus_slave.c **** // 			 	0x01 0x03 0x00 0x00 0x00 0x02 0xC4 0x0B 	lendo dois regs no device 1 addr 2
 256:uc_libc_dev/protocol/modbus/modbus_slave.c **** //				0x04 0x03 0x00 0x0F 0x00 0x02 0xF4 0x5D		lendo dois regs no device 4 addr 2
 257:uc_libc_dev/protocol/modbus/modbus_slave.c **** int modbus_SlaveProcess(void) {
 199              		.loc 1 257 0
 200              		.cfi_startproc
 201              		@ args = 0, pretend = 0, frame = 8
 202              		@ frame_needed = 0, uses_anonymous_args = 0
 203 0000 F7B5     		push	{r0, r1, r2, r4, r5, r6, r7, lr}
 204              	.LCFI3:
 205              		.cfi_def_cfa_offset 32
 206              		.cfi_offset 4, -20
 207              		.cfi_offset 5, -16
 208              		.cfi_offset 6, -12
 209              		.cfi_offset 7, -8
 210              		.cfi_offset 14, -4
 258:uc_libc_dev/protocol/modbus/modbus_slave.c ****    	int len = modbus.byteAvailable();
 211              		.loc 1 258 0
 212 0002 564C     		ldr	r4, .L49
 213 0004 E368     		ldr	r3, [r4, #12]
 214 0006 9847     		blx	r3
 215              	.LVL10:
 216 0008 2646     		mov	r6, r4
 259:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 260:uc_libc_dev/protocol/modbus/modbus_slave.c ****    	// se não há nada no buffer não fazemos nada
 261:uc_libc_dev/protocol/modbus/modbus_slave.c ****     if ( len == 0 ) {
 217              		.loc 1 261 0
 218 000a 10B9     		cbnz	r0, .L14
 262:uc_libc_dev/protocol/modbus/modbus_slave.c ****     	modbus.lastLenBuffer = 0;
 219              		.loc 1 262 0
 220 000c C4F81C01 		str	r0, [r4, #284]
 263:uc_libc_dev/protocol/modbus/modbus_slave.c ****         return pdFAIL;
 221              		.loc 1 263 0
 222 0010 A1E0     		b	.L15
 223              	.L14:
 264:uc_libc_dev/protocol/modbus/modbus_slave.c ****     }
 265:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 266:uc_libc_dev/protocol/modbus/modbus_slave.c ****     // reinicia o timeout caso o buffer de recepção vai aumentando
 267:uc_libc_dev/protocol/modbus/modbus_slave.c ****     // se estourar o buffer o driver serial se encarrega disso, e neste caso não processamos nada
 268:uc_libc_dev/protocol/modbus/modbus_slave.c ****     // mesmo com o buffer cheio com dados lixo os dados serão todos processados e o CRC não vai bat
 269:uc_libc_dev/protocol/modbus/modbus_slave.c ****     if (modbus.lastLenBuffer != len) {
 224              		.loc 1 269 0
 225 0012 D4F81C31 		ldr	r3, [r4, #284]
 226 0016 9842     		cmp	r0, r3
 227 0018 04D0     		beq	.L16
 270:uc_libc_dev/protocol/modbus/modbus_slave.c ****     	modbus.lastLenBuffer = len;
 271:uc_libc_dev/protocol/modbus/modbus_slave.c ****         modbus.timeout = 100; // valor 10 funciona bem entre 2400 a 115200 bps. Valor 5 funcionou b
 228              		.loc 1 271 0
 229 001a 6423     		movs	r3, #100
 270:uc_libc_dev/protocol/modbus/modbus_slave.c ****     	modbus.lastLenBuffer = len;
 230              		.loc 1 270 0
 231 001c C4F81C01 		str	r0, [r4, #284]
 232              		.loc 1 271 0
 233 0020 6361     		str	r3, [r4, #20]
 234 0022 97E0     		b	.L29
 235              	.L16:
 272:uc_libc_dev/protocol/modbus/modbus_slave.c ****         									// para boudrate menores pode ser que devemos aumetar esse valor.
 273:uc_libc_dev/protocol/modbus/modbus_slave.c ****         									// Recomendo fazer uma macro associado ao boudrate da serial
 274:uc_libc_dev/protocol/modbus/modbus_slave.c ****         return pdFAIL;
 275:uc_libc_dev/protocol/modbus/modbus_slave.c ****     }
 276:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 277:uc_libc_dev/protocol/modbus/modbus_slave.c ****     // somente vamos processar quando estourar o timeout de recepção
 278:uc_libc_dev/protocol/modbus/modbus_slave.c ****     if (modbus.timeout) return pdFAIL;
 236              		.loc 1 278 0
 237 0024 6569     		ldr	r5, [r4, #20]
 238 0026 002D     		cmp	r5, #0
 239 0028 40F09480 		bne	.L29
 240              	.LBB8:
 241              	.LBB9:
 164:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		if (len > 255) return errMODBUS_BUFRX_OVERFLOW;
 242              		.loc 1 164 0
 243 002c 2746     		mov	r7, r4
 244              	.LBE9:
 245              	.LBE8:
 279:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 280:uc_libc_dev/protocol/modbus/modbus_slave.c ****    	// processando o pacote
 281:uc_libc_dev/protocol/modbus/modbus_slave.c ****    	modbus.lastLenBuffer = 0;
 246              		.loc 1 281 0
 247 002e C4F81C51 		str	r5, [r4, #284]
 248              	.LVL11:
 249              	.L17:
 250              	.LBB11:
 251              	.LBB10:
 164:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		if (len > 255) return errMODBUS_BUFRX_OVERFLOW;
 252              		.loc 1 164 0
 253 0032 B368     		ldr	r3, [r6, #8]
 254 0034 0DF10700 		add	r0, sp, #7
 255 0038 9847     		blx	r3
 256              	.LVL12:
 257 003a 0128     		cmp	r0, #1
 258 003c 474C     		ldr	r4, .L49
 259 003e 08D1     		bne	.L48
 165:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		query[len++] = ch;
 260              		.loc 1 165 0
 261 0040 B5F5807F 		cmp	r5, #256
 262 0044 1BD0     		beq	.L20
 166:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		#if (MODBUS_USE_DEBUG == pdON)
 263              		.loc 1 166 0
 264 0046 0135     		adds	r5, r5, #1
 265              	.LVL13:
 266 0048 9DF80720 		ldrb	r2, [sp, #7]	@ zero_extendqisi2
 267 004c 7B19     		adds	r3, r7, r5
 268 004e DA75     		strb	r2, [r3, #23]
 269 0050 EFE7     		b	.L17
 270              	.L48:
 173:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		#if (MODBUS_USE_DEBUG == pdON)
 271              		.loc 1 173 0
 272 0052 032D     		cmp	r5, #3
 273 0054 13DD     		ble	.L20
 183:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	uint crc = crc16_MODBUS(query, len);
 274              		.loc 1 183 0
 275 0056 023D     		subs	r5, r5, #2
 276              	.LVL14:
 184:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	uint crc_rx = ( query[len+1] << 8) | (query[len]);
 277              		.loc 1 184 0
 278 0058 2946     		mov	r1, r5
 279 005a 04F11800 		add	r0, r4, #24
 280 005e FFF7FEFF 		bl	crc16_MODBUS
 281              	.LVL15:
 185:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 282              		.loc 1 185 0
 283 0062 6219     		adds	r2, r4, r5
 196:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 284              		.loc 1 196 0
 285 0064 517E     		ldrb	r1, [r2, #25]	@ zero_extendqisi2
 286 0066 137E     		ldrb	r3, [r2, #24]	@ zero_extendqisi2
 287 0068 43EA0123 		orr	r3, r3, r1, lsl #8
 288 006c 9842     		cmp	r0, r3
 289 006e 06D1     		bne	.L20
 290              	.LVL16:
 291              	.LBE10:
 292              	.LBE11:
 282:uc_libc_dev/protocol/modbus/modbus_slave.c ****     #if (MODBUS_USE_DEBUG == pdON)
 283:uc_libc_dev/protocol/modbus/modbus_slave.c ****     plognp("MODBUS: proc len %d"CMD_TERMINATOR, len);
 284:uc_libc_dev/protocol/modbus/modbus_slave.c ****     #endif
 285:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 286:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	len = modbus_GetPacket(modbus.query);
 287:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 288:uc_libc_dev/protocol/modbus/modbus_slave.c ****     // se houve erro de CRC ou overflow do buffer, ou o endereço na mensagem é para este dispositiv
 289:uc_libc_dev/protocol/modbus/modbus_slave.c ****     // o menor pacote é 4 bytes (1 byte ID, 1 byte função e 2 bytes CRC), porém no modbus.query não
 290:uc_libc_dev/protocol/modbus/modbus_slave.c ****     if ( (len < 1) || (modbus.query[0] != modbus.slaveID) ) {
 293              		.loc 1 290 0
 294 0070 002D     		cmp	r5, #0
 295 0072 04DD     		ble	.L20
 296              		.loc 1 290 0 is_stmt 0 discriminator 1
 297 0074 327E     		ldrb	r2, [r6, #24]	@ zero_extendqisi2
 298 0076 3368     		ldr	r3, [r6]
 299 0078 2546     		mov	r5, r4
 300 007a 9A42     		cmp	r2, r3
 301 007c 02D0     		beq	.L21
 302              	.L20:
 291:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	    #if (MODBUS_USE_DEBUG == pdON)
 292:uc_libc_dev/protocol/modbus/modbus_slave.c ****     	plognp("MODBUS: pck error"CMD_TERMINATOR);
 293:uc_libc_dev/protocol/modbus/modbus_slave.c ****     	#endif
 294:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		modbus.flushRX();
 303              		.loc 1 294 0 is_stmt 1
 304 007e 3369     		ldr	r3, [r6, #16]
 305 0080 9847     		blx	r3
 306              	.LVL17:
 307 0082 67E0     		b	.L29
 308              	.L21:
 295:uc_libc_dev/protocol/modbus/modbus_slave.c ****     	return pdFAIL;
 296:uc_libc_dev/protocol/modbus/modbus_slave.c ****     };
 297:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 298:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	int ret;
 299:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	uint addrInit; // Não podemos colocar a captura do addr aqui porque pode haver comandos simples
 300:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 301:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.cmd = modbus.query[1];
 309              		.loc 1 301 0
 310 0084 637E     		ldrb	r3, [r4, #25]	@ zero_extendqisi2
 302:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 303:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	// checa se a função é de leitura dos registradores, processa caso há função anexada
 304:uc_libc_dev/protocol/modbus/modbus_slave.c ****     if ( (modbus.cmd == modbusCMD_READ_REGISTERS) && (modbus.read_regs != NULL)) {
 311              		.loc 1 304 0
 312 0086 032B     		cmp	r3, #3
 301:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 313              		.loc 1 301 0
 314 0088 C4F81831 		str	r3, [r4, #280]
 315              		.loc 1 304 0
 316 008c 25D1     		bne	.L22
 317              		.loc 1 304 0 is_stmt 0 discriminator 1
 318 008e D4F82071 		ldr	r7, [r4, #288]
 319 0092 002F     		cmp	r7, #0
 320 0094 57D0     		beq	.L23
 321              	.LVL18:
 305:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		addrInit = ((uint) modbus.query[2] << 8) + (uint)modbus.query[3];
 306:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		len = ((int) modbus.query[4] << 8) + (int)modbus.query[5];
 307:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 308:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		// chamar a função externa para capturar os dados
 309:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		// 	passo o endereço do buffer a partir do 4º byte, os tres primeiros são reservador para ID, fun
 310:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		ret = modbus.read_regs(addrInit, &modbus.query[3], len);
 322              		.loc 1 310 0 is_stmt 1
 323 0096 2146     		mov	r1, r4
 306:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 324              		.loc 1 306 0
 325 0098 637F     		ldrb	r3, [r4, #29]	@ zero_extendqisi2
 326 009a 257F     		ldrb	r5, [r4, #28]	@ zero_extendqisi2
 327              		.loc 1 310 0
 328 009c A07E     		ldrb	r0, [r4, #26]	@ zero_extendqisi2
 306:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 329              		.loc 1 306 0
 330 009e 03EB0525 		add	r5, r3, r5, lsl #8
 331              	.LVL19:
 332              		.loc 1 310 0
 333 00a2 11F81B3F 		ldrb	r3, [r1, #27]!	@ zero_extendqisi2
 334 00a6 2A46     		mov	r2, r5
 335 00a8 03EB0020 		add	r0, r3, r0, lsl #8
 336 00ac B847     		blx	r7
 337              	.LVL20:
 311:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		if (ret == modbusNO_ERROR)
 338              		.loc 1 311 0
 339 00ae 0028     		cmp	r0, #0
 340 00b0 4AD1     		bne	.L45
 341              	.LVL21:
 342              	.LBB12:
 343              	.LBB13:
 233:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	query[1] = modbus.cmd;
 344              		.loc 1 233 0
 345 00b2 2368     		ldr	r3, [r4]
 235:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	uint crc = crc16_MODBUS(query, (2*len)+3);
 346              		.loc 1 235 0
 347 00b4 6D00     		lsls	r5, r5, #1
 348              	.LVL22:
 233:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	query[1] = modbus.cmd;
 349              		.loc 1 233 0
 350 00b6 2376     		strb	r3, [r4, #24]
 234:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	query[2] = (u8)(2*len);
 351              		.loc 1 234 0
 352 00b8 D4F81831 		ldr	r3, [r4, #280]
 236:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	query[(2*len)+3] = (u8)(crc&0xFF);
 353              		.loc 1 236 0
 354 00bc E91C     		adds	r1, r5, #3
 234:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	query[2] = (u8)(2*len);
 355              		.loc 1 234 0
 356 00be 6376     		strb	r3, [r4, #25]
 235:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	uint crc = crc16_MODBUS(query, (2*len)+3);
 357              		.loc 1 235 0
 358 00c0 A576     		strb	r5, [r4, #26]
 236:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	query[(2*len)+3] = (u8)(crc&0xFF);
 359              		.loc 1 236 0
 360 00c2 04F11800 		add	r0, r4, #24
 361              	.LVL23:
 362 00c6 FFF7FEFF 		bl	crc16_MODBUS
 363              	.LVL24:
 237:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	query[(2*len)+4] = (u8)(crc>>8);
 364              		.loc 1 237 0
 365 00ca 6319     		adds	r3, r4, r5
 366 00cc D876     		strb	r0, [r3, #27]
 238:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.puts(query, (2*len)+5);
 367              		.loc 1 238 0
 368 00ce 000A     		lsrs	r0, r0, #8
 369              	.LVL25:
 370 00d0 1877     		strb	r0, [r3, #28]
 239:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 371              		.loc 1 239 0
 372 00d2 691D     		adds	r1, r5, #5
 373 00d4 6368     		ldr	r3, [r4, #4]
 374 00d6 89B2     		uxth	r1, r1
 375 00d8 12E0     		b	.L44
 376              	.LVL26:
 377              	.L22:
 378              	.LBE13:
 379              	.LBE12:
 312:uc_libc_dev/protocol/modbus/modbus_slave.c **** 			modbus_SendPacketRegs(modbus.query, len); // Envia os valores dos registradores
 313:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		else
 314:uc_libc_dev/protocol/modbus/modbus_slave.c **** 			modbus_SendPacketException(ret);
 315:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 316:uc_libc_dev/protocol/modbus/modbus_slave.c ****     // checa se a função é de escrita no registrador, processa caso há função anexada
 317:uc_libc_dev/protocol/modbus/modbus_slave.c ****     } else if ( (modbus.cmd == modbusCMD_WRITE_REGISTER) && (modbus.write_reg != NULL)) {
 380              		.loc 1 317 0
 381 00da 062B     		cmp	r3, #6
 382 00dc 14D1     		bne	.L26
 383              		.loc 1 317 0 is_stmt 0 discriminator 1
 384 00de D4F82421 		ldr	r2, [r4, #292]
 385 00e2 002A     		cmp	r2, #0
 386 00e4 2FD0     		beq	.L23
 387              	.LVL27:
 388              	.LBB14:
 318:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		// chamar a função externa gravar o registrador
 319:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		addrInit = ((uint) modbus.query[2] << 8) + (uint)modbus.query[3];
 320:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		u16 data = ((u16) modbus.query[4] << 8) + (u16)modbus.query[5];
 321:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		ret = modbus.write_reg(addrInit, data);
 389              		.loc 1 321 0 is_stmt 1
 390 00e6 637F     		ldrb	r3, [r4, #29]	@ zero_extendqisi2
 391 00e8 217F     		ldrb	r1, [r4, #28]	@ zero_extendqisi2
 392 00ea A07E     		ldrb	r0, [r4, #26]	@ zero_extendqisi2
 393 00ec 03EB0121 		add	r1, r3, r1, lsl #8
 394 00f0 E37E     		ldrb	r3, [r4, #27]	@ zero_extendqisi2
 395 00f2 89B2     		uxth	r1, r1
 396 00f4 03EB0020 		add	r0, r3, r0, lsl #8
 397 00f8 9047     		blx	r2
 398              	.LVL28:
 322:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		if (ret == modbusNO_ERROR)
 399              		.loc 1 322 0
 400 00fa 28BB     		cbnz	r0, .L45
 323:uc_libc_dev/protocol/modbus/modbus_slave.c **** 			modbus.puts(modbus.query, 8); // retorna o eco do pedido sinalizando ao mestre que tudo ocorreu 
 401              		.loc 1 323 0
 402 00fc 6368     		ldr	r3, [r4, #4]
 403 00fe 0821     		movs	r1, #8
 404              	.LVL29:
 405              	.L44:
 406 0100 04F11800 		add	r0, r4, #24
 407              	.L46:
 408 0104 9847     		blx	r3
 409              	.LVL30:
 410 0106 21E0     		b	.L25
 411              	.L26:
 412              	.LBE14:
 324:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		else
 325:uc_libc_dev/protocol/modbus/modbus_slave.c **** 			modbus_SendPacketException(ret);
 326:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 327:uc_libc_dev/protocol/modbus/modbus_slave.c ****     // checa se a função é de escrita nos registradores, processa caso há função anexada
 328:uc_libc_dev/protocol/modbus/modbus_slave.c ****     } else if ( (modbus.cmd == modbusCMD_WRITE_REGISTERS) && (modbus.write_regs != NULL)) {
 413              		.loc 1 328 0
 414 0108 102B     		cmp	r3, #16
 415 010a 1CD1     		bne	.L23
 416              		.loc 1 328 0 is_stmt 0 discriminator 1
 417 010c D4F82841 		ldr	r4, [r4, #296]
 418 0110 CCB1     		cbz	r4, .L23
 419              	.LVL31:
 329:uc_libc_dev/protocol/modbus/modbus_slave.c ****     	addrInit = ((uint) modbus.query[2] << 8) + (uint)modbus.query[3];
 330:uc_libc_dev/protocol/modbus/modbus_slave.c ****     	len = ((int) modbus.query[4] << 8) + (int)modbus.query[5];
 420              		.loc 1 330 0 is_stmt 1
 421 0112 6B7F     		ldrb	r3, [r5, #29]	@ zero_extendqisi2
 422 0114 2A7F     		ldrb	r2, [r5, #28]	@ zero_extendqisi2
 423 0116 03EB0222 		add	r2, r3, r2, lsl #8
 424              	.LVL32:
 331:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		// se a quantidade de registradores não corresponde com a quantidade de bytes no buffer retorna e
 332:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		if (2*len != modbus.query[6]) ret = modbusILLEGAL_FUNCTION;
 425              		.loc 1 332 0
 426 011a AB7F     		ldrb	r3, [r5, #30]	@ zero_extendqisi2
 427 011c B3EB420F 		cmp	r3, r2, lsl #1
 428 0120 11D1     		bne	.L23
 333:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		else
 334:uc_libc_dev/protocol/modbus/modbus_slave.c **** 			// chamar a função externa gravar nos registradores
 335:uc_libc_dev/protocol/modbus/modbus_slave.c **** 			// 	passo o endereço do buffer a partir do 7º byte onde começa os dados
 336:uc_libc_dev/protocol/modbus/modbus_slave.c **** 			ret = modbus.write_regs(addrInit, &modbus.query[7], len);
 429              		.loc 1 336 0
 430 0122 A87E     		ldrb	r0, [r5, #26]	@ zero_extendqisi2
 431 0124 EB7E     		ldrb	r3, [r5, #27]	@ zero_extendqisi2
 432 0126 0E49     		ldr	r1, .L49+4
 433 0128 03EB0020 		add	r0, r3, r0, lsl #8
 434 012c A047     		blx	r4
 435              	.LVL33:
 337:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		if (ret == modbusNO_ERROR) { // retorna o eco do pedido sinalizando ao mestre que tudo ocorreu be
 436              		.loc 1 337 0
 437 012e 58B9     		cbnz	r0, .L45
 438              	.LBB15:
 338:uc_libc_dev/protocol/modbus/modbus_slave.c **** 			uint crc = crc16_MODBUS(modbus.query, 6);
 439              		.loc 1 338 0
 440 0130 0621     		movs	r1, #6
 441 0132 0C48     		ldr	r0, .L49+8
 442              	.LVL34:
 443 0134 FFF7FEFF 		bl	crc16_MODBUS
 444              	.LVL35:
 339:uc_libc_dev/protocol/modbus/modbus_slave.c **** 			modbus.query[6] = (u8)(crc&0xFF);
 445              		.loc 1 339 0
 446 0138 A877     		strb	r0, [r5, #30]
 340:uc_libc_dev/protocol/modbus/modbus_slave.c **** 			modbus.query[7] = (u8)(crc>>8);
 447              		.loc 1 340 0
 448 013a 000A     		lsrs	r0, r0, #8
 449              	.LVL36:
 450 013c E877     		strb	r0, [r5, #31]
 341:uc_libc_dev/protocol/modbus/modbus_slave.c **** 			modbus.puts(modbus.query, 8);
 451              		.loc 1 341 0
 452 013e 6B68     		ldr	r3, [r5, #4]
 453 0140 0821     		movs	r1, #8
 454 0142 0848     		ldr	r0, .L49+8
 455 0144 DEE7     		b	.L46
 456              	.L23:
 457              	.LBE15:
 342:uc_libc_dev/protocol/modbus/modbus_slave.c **** 		} else
 343:uc_libc_dev/protocol/modbus/modbus_slave.c **** 			modbus_SendPacketException(ret);
 344:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	} else {
 345:uc_libc_dev/protocol/modbus/modbus_slave.c ****     	// envia uma mensagem de exceção ao mestre que este dispositivo não suporta tal função
 346:uc_libc_dev/protocol/modbus/modbus_slave.c ****     	modbus_SendPacketException(modbusILLEGAL_FUNCTION);
 458              		.loc 1 346 0
 459 0146 0120     		movs	r0, #1
 460              	.L45:
 461 0148 FFF7FEFF 		bl	modbus_SendPacketException
 462              	.LVL37:
 463              	.L25:
 347:uc_libc_dev/protocol/modbus/modbus_slave.c ****     }
 348:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 349:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	modbus.flushRX();
 464              		.loc 1 349 0
 465 014c 3369     		ldr	r3, [r6, #16]
 466 014e 9847     		blx	r3
 467              	.LVL38:
 350:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	return pdPASS;
 468              		.loc 1 350 0
 469 0150 0120     		movs	r0, #1
 470 0152 00E0     		b	.L15
 471              	.L29:
 278:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 472              		.loc 1 278 0
 473 0154 0020     		movs	r0, #0
 474              	.L15:
 351:uc_libc_dev/protocol/modbus/modbus_slave.c **** }
 475              		.loc 1 351 0
 476 0156 03B0     		add	sp, sp, #12
 477              	.LCFI4:
 478              		.cfi_def_cfa_offset 20
 479              		@ sp needed
 480 0158 F0BD     		pop	{r4, r5, r6, r7, pc}
 481              	.L50:
 482 015a 00BF     		.align	2
 483              	.L49:
 484 015c 00000000 		.word	.LANCHOR0
 485 0160 1F000000 		.word	.LANCHOR0+31
 486 0164 18000000 		.word	.LANCHOR0+24
 487              		.cfi_endproc
 488              	.LFE6:
 490              		.section	.text.modbus_SlaveTick,"ax",%progbits
 491              		.align	1
 492              		.global	modbus_SlaveTick
 493              		.thumb
 494              		.thumb_func
 496              	modbus_SlaveTick:
 497              	.LFB7:
 352:uc_libc_dev/protocol/modbus/modbus_slave.c **** 
 353:uc_libc_dev/protocol/modbus/modbus_slave.c **** //-------------------------------------------------------------------------------------------------
 354:uc_libc_dev/protocol/modbus/modbus_slave.c **** // Esta função deve ser chamada a cada 1ms
 355:uc_libc_dev/protocol/modbus/modbus_slave.c **** //-------------------------------------------------------------------------------------------------
 356:uc_libc_dev/protocol/modbus/modbus_slave.c **** void modbus_SlaveTick(void) {
 498              		.loc 1 356 0
 499              		.cfi_startproc
 500              		@ args = 0, pretend = 0, frame = 0
 501              		@ frame_needed = 0, uses_anonymous_args = 0
 502              		@ link register save eliminated.
 357:uc_libc_dev/protocol/modbus/modbus_slave.c **** 	if (modbus.timeout) modbus.timeout--;
 503              		.loc 1 357 0
 504 0000 034B     		ldr	r3, .L56
 505 0002 5A69     		ldr	r2, [r3, #20]
 506 0004 12B1     		cbz	r2, .L51
 507              		.loc 1 357 0 is_stmt 0 discriminator 1
 508 0006 5A69     		ldr	r2, [r3, #20]
 509 0008 013A     		subs	r2, r2, #1
 510 000a 5A61     		str	r2, [r3, #20]
 511              	.L51:
 512 000c 7047     		bx	lr
 513              	.L57:
 514 000e 00BF     		.align	2
 515              	.L56:
 516 0010 00000000 		.word	.LANCHOR0
 517              		.cfi_endproc
 518              	.LFE7:
 520              		.section	.bss.modbus,"aw",%nobits
 521              		.align	2
 522              		.set	.LANCHOR0,. + 0
 525              	modbus:
 526 0000 00000000 		.space	300
 526      00000000 
 526      00000000 
 526      00000000 
 526      00000000 
 527              		.text
 528              	.Letext0:
 529              		.file 2 "uc_libc_dev/_libdef/uc_libdefs.h"
 530              		.file 3 "uc_libc_dev/crc/crc.h"
DEFINED SYMBOLS
                            *ABS*:00000000 modbus_slave.c
D:\_temp\_windows\cciQKpll.s:20     .text.modbus_SendPacketException:00000000 $t
D:\_temp\_windows\cciQKpll.s:24     .text.modbus_SendPacketException:00000000 modbus_SendPacketException
D:\_temp\_windows\cciQKpll.s:87     .text.modbus_SendPacketException:00000030 $d
D:\_temp\_windows\cciQKpll.s:92     .text.modbus_SlaveInit:00000000 $t
D:\_temp\_windows\cciQKpll.s:97     .text.modbus_SlaveInit:00000000 modbus_SlaveInit
D:\_temp\_windows\cciQKpll.s:133    .text.modbus_SlaveInit:00000024 $d
D:\_temp\_windows\cciQKpll.s:138    .text.modbus_SlaveSetID:00000000 $t
D:\_temp\_windows\cciQKpll.s:143    .text.modbus_SlaveSetID:00000000 modbus_SlaveSetID
D:\_temp\_windows\cciQKpll.s:158    .text.modbus_SlaveSetID:00000008 $d
D:\_temp\_windows\cciQKpll.s:163    .text.modbus_SlaveAppendFunctions:00000000 $t
D:\_temp\_windows\cciQKpll.s:168    .text.modbus_SlaveAppendFunctions:00000000 modbus_SlaveAppendFunctions
D:\_temp\_windows\cciQKpll.s:187    .text.modbus_SlaveAppendFunctions:00000010 $d
D:\_temp\_windows\cciQKpll.s:192    .text.modbus_SlaveProcess:00000000 $t
D:\_temp\_windows\cciQKpll.s:197    .text.modbus_SlaveProcess:00000000 modbus_SlaveProcess
D:\_temp\_windows\cciQKpll.s:484    .text.modbus_SlaveProcess:0000015c $d
D:\_temp\_windows\cciQKpll.s:491    .text.modbus_SlaveTick:00000000 $t
D:\_temp\_windows\cciQKpll.s:496    .text.modbus_SlaveTick:00000000 modbus_SlaveTick
D:\_temp\_windows\cciQKpll.s:516    .text.modbus_SlaveTick:00000010 $d
D:\_temp\_windows\cciQKpll.s:521    .bss.modbus:00000000 $d
D:\_temp\_windows\cciQKpll.s:525    .bss.modbus:00000000 modbus
                     .debug_frame:00000010 $d

UNDEFINED SYMBOLS
crc16_MODBUS
